package com.sanhuo.idms.dao.provider;

import com.sanhuo.idms.bo.StudentPageBO;
import com.sanhuo.idms.entity.Student;
import com.sanhuo.triild.provider.SQL;
import com.sanhuo.utils.basic.BeanUtil;
import com.sanhuo.utils.basic.ReflectionUtil;
import com.sanhuo.utils.basic.StringUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class StudentProvider extends SQL {
    private final String TABLENAME = "student";

    public String findStudentByBO(StudentPageBO pageBO) {
        SQL sql = getSQL();
        sql.select("*").from(TABLENAME);
        if (StringUtil.isNotBlank(pageBO.getKeyword())) {
            sql.where("name like #{keyword} ");
        }
        pageBO.setKeyword("%" + pageBO.getKeyword() + "%");
        return sql.build();
    }

    public String listAll(StudentPageBO bo) {
        SQL sql = getSQL();
        sql.before(" select s.id id,s.name name,s.gender gender,co.name college,c.collegeId collegeId ,c.clasGrade grade,c.clasNum clas,r.num roomNum,t.num tungNum  from student s join room r join dorm d join college co join clas c join tung t" +
                " on s.clasId = c.id and c.collegeId = co.id and s.dormId = d.id and d.roomId = r.id and d.tungId = t.id where 1=1");
        if (StringUtil.isNotBlank(bo.getKeyword())) {
            sql.where("s.name like #{keyword} ");
        }
        if (StringUtil.isNotBlank(bo.getCollege())) {
            sql.where("c.collegeId = #{college}");
        }
        if (StringUtil.isNotBlank(bo.getGender())) {
            sql.where("s.gender = #{gender}");
        }
        if (StringUtil.isNotBlank(bo.getClas())) {
            sql.where("s.clasId = #{clas}");
        }
//        bo.setClas("%" + bo.getClas() + "%");
        bo.setKeyword("%" + bo.getKeyword() + "%");
//        sql.orderBy("collegeId", "asc");
        return sql.build();
    }

    public String update(Student student) {
        SQL sql = getSQL();
        sql.update(TABLENAME);
        Field[] fields = ReflectionUtil.getFieldList(student);
        List<String> updateColumn = new ArrayList<>();
        for (Field field : fields) {
            if (BeanUtil.isNotNull(ReflectionUtil.getFieldValue(student, field))) {
                updateColumn.add(field.getName() + "= #{" + field.getName() + "}");
            }
        }
        sql.set(updateColumn.toArray(new String[updateColumn.size()])).where("id = #{id}");
        return sql.build();
    }

    public String insert(Student student) {
        List<String> condition = new ArrayList<>();
        List<String> condition_ps = new ArrayList<>();
        Field[] fields = ReflectionUtil.getFieldList(student);
        for (Field field : fields) {
            if (BeanUtil.isNotNull(ReflectionUtil.getFieldValue(student, field))) {
                condition.add(field.getName());
                condition_ps.add("#{" + field.getName() + "}");
            }
        }
        SQL sql = getSQL();
        sql.insert("student").Insert_column(condition.toArray(new String[condition.size()]))
                .value(condition_ps.toArray(new String[condition_ps.size()]));
        return sql.build();
    }
}
